// https://www.lintcode.com/problem/sliding-window-maximum/description

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int> &nums, int k) {
        vector<int> ans;
        if(nums.empty() || nums.size() < k){
            return ans;
        }
        
        deque<int> q;
        for(int i = 0; i < nums.size(); i++){
            while(!q.empty() && nums[i] >= nums[q.front()]){
                q.pop_front();
            }
            q.push_front(i);
            if(i >= k - 1){
                ans.push_back(nums[q.back()]);
            }
            while(!q.empty() && q.back() <= i - k + 1){
                q.pop_back();
            }
        }
        return ans;
    }
};